//
//  BaseViewController+HUB.m
//  Traffic
//
//  Created by CruItaly on 2017/10/28.
//  Copyright © 2017年 com.infohold.cn. All rights reserved.
//

#import "BaseViewController+HUB.h"
#import "MBProgressHUD.h" 
#import "UIView+Toast.h"
@implementation UIViewController (HUB)

- (UIView *)toppestView{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    return keyWindow;
}

- (void)showHub:(NSString *)text{
   MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[self toppestView] animated:YES];
    hub.label.text = text;
} 
- (void)hiddenHub{
    [MBProgressHUD hideHUDForView:[self toppestView] animated:YES];
}
- (void)showHubAuto:(NSString *)text{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[self toppestView] animated:YES];
    hub.label.text = text;
    
    [hub hideAnimated:YES afterDelay:2.0];
    [hub removeFromSuperViewOnHide];
}
- (void)showHubAuto:(NSString *)text image:(NSString *)imageName{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[self toppestView] animated:YES];
    hub.label.text = text;
    
    // 显示模式,改成customView,即显示自定义图片(mode设置,必须写在customView赋值之前)
    hub.mode = MBProgressHUDModeCustomView;
    // 设置要显示 的自定义的图片
    hub.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [hub hideAnimated:YES afterDelay:2.0];
    [hub removeFromSuperViewOnHide];
  
}

- (void) showTip:(NSString *)tips cancelText:(NSString *)cancelText confirmText:(NSString *) confirmText Confirm:(ConfirmBlock)confirmClick Cancel:(CancelBlock) cancelClick{
    // 1.弹框提醒
    // 初始化对话框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:tips preferredStyle:UIAlertControllerStyleAlert];
   
    
    if(![cancelText isEqualToString:@""]){
        [alert addAction:[UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            cancelClick();
        }]];
    }
    
    [alert addAction:[UIAlertAction actionWithTitle:confirmText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if(confirmClick){
             confirmClick();
        }
       
    }]];
    
    // 弹出对话框
    [self presentViewController:alert animated:true completion:nil];
}

- (void)showToast:(NSString *)msg{
    [[self toppestView] makeToast:msg];
}



- (void)setNavtitle:(NSString *)title{
    self.navigationItem.title = title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
}
- (void)setNavLeftImg:(NSString *)name{
    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuButton setFrame:CGRectMake(-20.0, 0.0, 44.0, 44.0)];
    [menuButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [menuButton setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    [view addSubview:menuButton];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = leftItem;
}

//添加左侧图标
- (void)tfAddLeftImg:(UITextField *)textField ImageName:(NSString *)imageName{
    textField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *phoneImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [phoneImage setFrame:CGRectMake(10, 5, 30, 30)];
    UIView *lv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    textField.center = lv.center;
    [lv addSubview:phoneImage];
    textField.leftView = lv;
}

- (void)backClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
